package org.fire.spring.boot.input;

import org.fire.spring.boot.input.tools.InputTools;
import org.junit.Test;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public class Application {

    private Map<String, List<String>> read(String file) throws Exception {
        Map<String, List<String>> block = new LinkedHashMap<>();
        URL url = this.getClass().getClassLoader().getResource(file);
        Stream<String> stream = Files.lines(Paths.get(url.toURI()));
        stream.forEach(t -> {
            String[] line = t.split("\t");
            String blockName = line[0].trim();
            String code = line[1].trim();
            if(!block.containsKey(blockName)) {
                block.put(blockName, new LinkedList<>());
            }
            block.get(blockName).add(code.trim());
        });
        stream.close();
        return block;
    }

    public void init(Robot robot) {
        InputTools.click(robot,100,100, 100);
        InputTools.key(robot, 200, KeyEvent.VK_F6);
        InputTools.key(robot, 0, KeyEvent.VK_DELETE, 250);
        robot.delay(5000);
    }

    private void moveToSelect(Robot robot, List<String> codes) {
        for(String code : codes) {
            InputTools.input(robot, 1000, code);
            InputTools.enter(robot, 200);
            InputTools.hotkey(robot, 100, KeyEvent.VK_ALT, KeyEvent.VK_Z);
        }
    }

    private void moveToBlock(Robot robot, String name) throws Exception {
        InputTools.key(robot, 100, KeyEvent.VK_F6);
        InputTools.hotkey(robot, 500, KeyEvent.VK_CONTROL, KeyEvent.VK_A);
        InputTools.hotkey(robot, 200, KeyEvent.VK_ALT, KeyEvent.VK_Y);
        // 全选中
        InputTools.key(robot, 100, KeyEvent.VK_TAB, 3);
        InputTools.space(robot,1000);
        // 加入到板块
        InputTools.hotkey(robot, 0, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
        InputTools.hotkey(robot, 0, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
        InputTools.space(robot,1000);
        // 新建板块
        InputTools.key(robot, 100, KeyEvent.VK_TAB, 3);
        InputTools.space(robot,1000);
        InputTools.inputGBK(robot, 200, name);
        InputTools.enter(robot, 200);
        // 确定
        InputTools.hotkey(robot, 0, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
        InputTools.hotkey(robot, 200, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
        InputTools.space(robot,1000);
    }

    @Test
    public void test() throws Exception {
        Robot robot = new Robot();
        Map<String, List<String>> block = read("block-stock.txt");
        for(Map.Entry<String, List<String>> entry : block.entrySet()) {
            String name = entry.getKey();
            List<String> codes = entry.getValue();
            System.out.println(name + " (size="+ codes.size()+"): " + codes);
            this.init(robot);
            this.moveToSelect(robot, codes);
            this.moveToBlock(robot, name);
        }
    }
}